"""字段配置模型"""
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey, JSON, func, UniqueConstraint
from app.database import Base


class FieldConfig(Base):
    """字段配置表"""
    __tablename__ = "field_configs"

    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey("scan_tasks.id", ondelete="CASCADE"), nullable=False, comment="任务ID")
    field_name = Column(String(100), nullable=False, comment="字段名称(唯一标识)")
    display_name = Column(String(200), nullable=False, comment="显示名称")
    field_type = Column(String(50), nullable=False, comment="字段类型: text/number/date/boolean/code/link")
    field_category = Column(String(50), nullable=False, comment="字段分类: record_extension/issue_extension")
    display_order = Column(Integer, default=0, comment="显示顺序")
    show_in_list = Column(Boolean, default=False, comment="是否在列表展示")
    show_in_detail = Column(Boolean, default=True, comment="是否在详情展示")
    searchable = Column(Boolean, default=False, comment="是否可搜索")
    sortable = Column(Boolean, default=False, comment="是否可排序")
    required = Column(Boolean, default=False, comment="是否必填")
    default_value = Column(JSON, comment="默认值")
    help_text = Column(Text, comment="帮助文本")
    created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 唯一约束
    __table_args__ = (UniqueConstraint("task_id", "field_name", name="uq_task_field"),)

